﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DashboardEnabler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        Storyboard busy;
        Storyboard idle;

        void makeBusy()
        {
            busy.Begin();
            this.ShowTitleBar = false;
        }

        void makeIdle()
        { 
            if (busy.GetCurrentState() != ClockState.Stopped)
            {
               busy.Stop();
                idle.Begin();
                this.ShowTitleBar = true;
            }
        }

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            busy = Resources["busy"] as Storyboard;
            idle = Resources["idle"] as Storyboard;
            makeBusy();
            await reAggregate();
            makeIdle();
        }

        private async Task aggregateFiles(string searchPattern, ListBox lb)
        {
            await Task.Run(() =>
            {
                //Thread.Sleep(10000);
                var files = Directory.GetFiles(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Constants.EA, Constants.TheSims3, Constants.Mods, Constants.Packages), searchPattern, SearchOption.AllDirectories);
                this.Dispatcher.Invoke(() =>
                {
                    lb.ItemsSource = files;
                    //makeIdle();
                });
            });
        }

        private async void reenableCCButton_Click(object sender, RoutedEventArgs e)
        {
            if (disabledCCs.SelectedItems.Count == 0) return;
            var collection = disabledCCs.SelectedItems.Cast<string>();
            makeBusy();
            await Task.Run(() =>
            {
                HashSet<string> set = new HashSet<string>();
                foreach (var item in collection)
                {
                    string currHash;
                    using (var cryptoProvider = new SHA1CryptoServiceProvider())
                    {
                        using (var stream = File.Open(item, FileMode.Open))
                        {
                            currHash = BitConverter
                                    .ToString(cryptoProvider.ComputeHash(stream));
                            //do something with hash
                        }
                    }
                    if (set.Contains(currHash) == false)
                    {
                        set.Add(currHash);
                        var fi = new FileInfo(item);
                        fi.MoveTo(System.IO.Path.Combine(fi.DirectoryName, fi.Name.Substring(0, fi.Name.LastIndexOf('.')) + Constants.PackageExtension));
                    }
                    this.Dispatcher.Invoke(() => busyNotice.Content = string.Format("applying changes"));
                }
            });
            await reAggregate();
            makeIdle();
        }

        private async void disableCC_Click(object sender, RoutedEventArgs e)
        {
            if (enabledCC.SelectedItems.Count == 0) return;
            var collection = enabledCC.SelectedItems.Cast<string>();
            makeBusy();
            await Task.Run(() =>
            {
                foreach (var item in collection)
                {
                    var fi = new FileInfo(item);
                    fi.MoveTo(System.IO.Path.Combine(fi.DirectoryName, fi.Name.Substring(0, fi.Name.LastIndexOf('.')) + Constants.DisabledPackageExtension));
                    this.Dispatcher.Invoke(() => busyNotice.Content = string.Format("applying changes"));
                }
            });
            await reAggregate();
            makeIdle();
        }

        private async Task reAggregate()
        {
            await aggregateFiles(Constants.DisabledPackagePattern, disabledCCs);
            await aggregateFiles(Constants.PackagepPattern, enabledCC);
        }
    }
}
